package github.flandre.examplemod.common.energy.block;

import com.mojang.datafixers.DSL;
import github.flandre.examplemod.common.energy.tileentity.FEDemoMachineTileEntity;
import github.flandre.examplemod.core.init.Itemgroup.ExampleGroup;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ObjectHolder;

import javax.annotation.Nonnull;

//机器方块的注册
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class FEDemoMachineBlock extends Block
{
    public static final String NAME = "examplemod:machine";

    @ObjectHolder(NAME)
    public static FEDemoMachineBlock BLOCK;

    @SubscribeEvent
    public static void onRegisterBlock(@Nonnull RegistryEvent.Register<Block> event)
    {
        event.getRegistry().register(new FEDemoMachineBlock().setRegistryName(NAME));
    }

    @SubscribeEvent
    public static void onRegisterItem(@Nonnull RegistryEvent.Register<Item> event)
    {
        event.getRegistry().register(new BlockItem(BLOCK, new Item.Properties().group(ExampleGroup.exampleGroup)).setRegistryName(NAME));
    }

    private FEDemoMachineBlock()
    {
        super(Block.Properties.create(Material.IRON).hardnessAndResistance(3));
    }

    //代表方块存在实体
    public boolean hasTileEntity(@Nonnull BlockState state)
    {
        return true;
    }

    //代表方块关联的是什么实体
    @Override
    public TileEntity createTileEntity(@Nonnull BlockState state, @Nonnull IBlockReader world)
    {
        FEDemoMachineTileEntity feDemoMachineTileEntity = FEDemoMachineTileEntity.TILE_ENTITY_TYPE.create();
        return feDemoMachineTileEntity;
    }


    //实体碰撞
    public void onEntityCollision(@Nonnull BlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Entity entity)
    {
        if (!world.isRemote && entity instanceof LivingEntity) //世界处于逻辑服务端 并且属于实体
        {
            LivingEntity livingEntity = (LivingEntity) entity;
            if (livingEntity.getHealth() < livingEntity.getMaxHealth()) //实体不是满血
            {
                TileEntity tileEntity = world.getTileEntity(pos);
                if (tileEntity instanceof FEDemoMachineTileEntity) //对应的方块实体是我们所期望的（使用 tileEntity instanceof FEDemoMachineTileEntity 判断）
                {
                    ((FEDemoMachineTileEntity) tileEntity).heal(livingEntity);
                }
            }
        }
    }


    //设置实体大小
    @Nonnull
    public VoxelShape getCollisionShape(@Nonnull BlockState state, @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull ISelectionContext context)
    {
        return Block.makeCuboidShape(0, 0, 0, 16, 15, 16);
    }


    //方块交互
    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);

    }
}